<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Excel;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function index(Request $request)
    {
    	$data =[];
    	
		
		return view('map',$data);
    }

    public function snapRoad(Request $request)
    {
        $page = $request->page?:1;
        $data =[];
        $points = [];

        if ($request->hasFile('excel')) {
            $filename = 'Trip.xlsx';
            $request->file('excel')->storeAs('excel',$filename);            
        }
        $list = Excel::load(storage_path('app/excel/Trip.xlsx'), function($reader) {})->get();
        
        
        $data['list'] = $this->paginate($list,5000,$page);
        $data['centerpoint'] = $data['list']->first();
        //dd($data['centerpoint']['lat']);
        return view('road',$data);
    }

    public function paginate($items, $perPage = 15, $page = null, $options = [])
    {
        $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
        $items = $items instanceof Collection ? $items : Collection::make($items);
        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
    }	
}
